#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "thread_pool.h"
#include <pthread.h>

int clock_destroy;		//时钟
pthread_mutex_t list_mutex = PTHREAD_MUTEX_INITIALIZER; // 链表操作互斥锁

typedef struct node
{
	int data; 
	
	struct node * prev; // 存储前一个节点地址
	struct node * next; // 存储后一个节点地址
}Dlink_t;

thread_pool pool;

// 初始化双向循环链表
Dlink_t *init_double_list(void)
{
	Dlink_t *head = (Dlink_t *)malloc(sizeof(Dlink_t));
	
	if(head != NULL)
	{
		head->next = head;
		head->prev = head;
	}
	
	return head;
}

// 创建双向循环链表节点（初始化数据，prev、next默认指向自己）
Dlink_t *create_node(int data)
{
	Dlink_t *new = (Dlink_t *)malloc(sizeof(Dlink_t));
	
	if(new != NULL)
	{
		new->data = data;
		new->next = new;
		new->prev = new;
	}
	
	return new;
}

// 尾插
void insert_tail(Dlink_t *head,Dlink_t *new)
{
	new->next = head;
	new->prev = head->prev;
	
	head->prev->next = new;
	head->prev = new;
}

// 删除（从链表中取出，未释放）
void remove_node(Dlink_t *node)
{
	node->prev->next = node->next;
	node->next->prev = node->prev;
	
	node->prev = node;
	node->next = node;
}

// 销毁节点
void destroy_node(Dlink_t *head)
{	
	if (head->next == head)
	{
		return;
	}
	
	Dlink_t *tmp = head->prev;
	
	// 销毁流程
	Dlink_t *destroy = tmp;
	tmp = destroy->next;  // 保留要销毁节点 的下一个
	
	remove_node(destroy);
	free(destroy);
}

// 销毁链表
void destroy_list(Dlink_t *head)
{
	Dlink_t *tmp = head->next;
	
	while(tmp != head)
	{
		Dlink_t *destroy = tmp;
		
		tmp = destroy->next;
		
		free(destroy);
	}
	
	free(head);
}

// 遍历函数（向后）
void display_list(Dlink_t *head)
{
	Dlink_t *tmp = head->next;
	
	while(tmp != head)
	{
		printf("%d->", tmp->data);
		
		tmp = tmp->next;
	}
	
	printf("\n");
}

//增加节点
void *add_new_node(void *arg)
{	
	int clock_add = 0;
	Dlink_t *head = (Dlink_t *)arg;
	static int x = 0;
	while (1)
	{	
		if(pool.shutdown)
            break;
		clock_add++;
		sleep(1);
		if(clock_add == 3)
		{	
			x++;
			Dlink_t *new = create_node(x);
			// 加锁保护链表操作
            pthread_mutex_lock(&list_mutex);
			insert_tail(head, new);
			display_list(head);		//遍历链表
			pthread_mutex_unlock(&list_mutex);

			printf("增加节点\n");
			clock_add = 0;
		}
	}
	
	
}

//删除节点
void *del_one_node(void *arg)
{	
	int clock_del = 0;
	Dlink_t *head = (Dlink_t *)arg;
	while (1)
	{	
		if(pool.shutdown)
            break;
		clock_del++;
		sleep(1);
		if (clock_del == 5)
		{	
			// 加锁保护链表操作
            pthread_mutex_lock(&list_mutex);
			destroy_node(head);
			display_list(head);		//遍历链表
			pthread_mutex_unlock(&list_mutex);

			printf("删除节点\n");
			clock_del = 0;
		}
	}
}

//结束
void *destroy_thread_pool(void *arg)
{	
	Dlink_t *head = (Dlink_t *)arg; 
	while (clock_destroy < 15)
	{	
		sleep(1);
		clock_destroy++;
		printf("%ds\n",clock_destroy);
		if (clock_destroy == 14)
		{
			pool.shutdown = 1;		//关闭线程池
		}	
	}

	printf("开始遍历剩余节点\n");
	pthread_mutex_lock(&list_mutex);
	display_list(head);		//遍历链表
	pthread_mutex_unlock(&list_mutex);
	sleep(1);

	pthread_mutex_lock(&list_mutex);
    destroy_list(head);			//销毁链表
    pthread_mutex_unlock(&list_mutex);

	//销毁互斥锁
    pthread_mutex_destroy(&list_mutex);
}

 

int main(void)
{
    Dlink_t *head = init_double_list();

	// 初始化线程池
	init_pool(&pool, 3);
	pool.shutdown = 0;	//初始化关闭

	// 投递计时任务
	add_task(&pool,add_new_node , head);
	add_task(&pool, del_one_node, head);
	add_task(&pool, destroy_thread_pool, head);

	while (!pool.shutdown)
		/*empty*/;

	sleep(1);
	pthread_exit(NULL);
	//destroy_pool(&pool);	//销毁线程池
	return 0;
}